home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / MISC / DTMFF110 / SB_DSP.ASM < prev    next >
Assembly Source File  |  1996-08-16  |  5KB  |  202 lines

  1. ;---------------------------------------------------------------------------
  2. ; SB_DSP.ASM -- Programmer's library for the Sound Blaster DSP interface
  3. ; ver 1.0  (Thursday, March 12, 1992)
  4. ;---------------------------------------------------------------------------
  5. ;  Copyright (C) 1992, Heath I Hunnicutt
  6. ;---------------------------------------------------------------------------
  7. ;  By: heathh@cco.caltech.edu -or- heathh@tybalt.cco.caltech.edu
  8. ;    aka hihunn@through.ugcs.caltech.edu (NOT preferred)
  9. ;---------------------------------------------------------------------------
  10. ;  Requires: Sound Blaster sound card or compatible
  11. ;         Turbo Assembler from Borland
  12. ;         A DMA library such as dma_code.asm by me
  13. ;---------------------------------------------------------------------------
  14. ;  Warning: Be sure that there is a card installed before calling these
  15. ;        routines.
  16. ;---------------------------------------------------------------------------
  17.  
  18.             IDEAL
  19.             MODEL medium
  20.  
  21.             DATASEG
  22. SB_IO_Port  DW 0220h    ;These values are kept here to avoid add
  23. DSP_Reset   DW 0226h    ;instructions in code that may be time-
  24. DSP_RDData  DW 022Ah    ;sensitive.
  25. DSP_Command DW 022Ch
  26. DSP_RDAvail DW 022Eh
  27.  
  28. DSP_WRData EQU DSP_Command    ;Aliases for the same variable name
  29. DSP_Status EQU DSP_Command
  30.  
  31.             CODESEG
  32.  
  33. PUBLIC _dsp_reset,_dsp_voice,_set_SB_address,_dsp_dma_prepare,_dsp_time
  34.  
  35. MACRO await_DSP
  36. LOCAL @@Wait_Ready
  37.      push ax dx
  38. @@Wait_Ready:
  39.      mov  dx,[DSP_Status]
  40.      in      al,dx
  41.      and  al,128
  42.       jnz @@Wait_Ready
  43.      pop  dx ax
  44. ENDM MACRO
  45.  
  46.  
  47. MACRO ten_microsec_delay
  48. LOCAL @@KT           ;If you know of a better, more accurate,
  49.      push cx           ;more reliable, just plain smarter, way
  50.      mov  cx,20000       ;to do this, PLEASE TELL ME.
  51. @@KT:               ;
  52.      nop           ;(The idea is to kill 10 microseconds.)
  53.      loop @@KT           ;
  54.      pop  cx           ;email: heathh@cco.caltech.edu
  55. ENDM ten_microsec_delay
  56.  
  57. ;---------------------------------------------------------------------------
  58. ; void far dsp_time(int pacing)
  59. ;---------------------------------------------------------------------------
  60. ; pacing = 255 - (1,000,000 / frequency)
  61. ;---------------------------------------------------------------------------
  62. PROC _dsp_time FAR
  63. ARG delay:WORD
  64.      push bp
  65.      mov  bp,sp
  66.      push ax dx
  67.  
  68.      await_DSP
  69.      mov  dx,[DSP_Command]
  70.      mov  al,040h
  71.      out  dx,al
  72.  
  73.      await_DSP
  74.      mov  dx,[DSP_Command]
  75.      mov  ax,[delay]
  76.      out  dx,al
  77.      pop  dx ax
  78.      pop  bp
  79.      ret
  80. ENDP _dsp_time
  81.  
  82.  
  83. ;---------------------------------------------------------------------------
  84. ; int far dsp_reset(void)
  85. ;---------------------------------------------------------------------------
  86. PROC    _dsp_reset FAR
  87.  
  88.     mov    dx,[DSP_Reset]
  89.     mov    al,1        ; Send a reset pulse
  90.     out    dx,al
  91.  
  92.     ten_microsec_delay
  93.  
  94.     mov    al,0
  95.     out    dx,al
  96.  
  97.     mov    cx,200        ; Check 200 times before failing
  98. @@Wait_Ready:
  99.     mov    dx,[DSP_RDAvail]
  100.     in    al,dx
  101.     and    al,128
  102.     jz    @@Not_Ready
  103.     mov    dx,[DSP_RDData]
  104.     in    al,dx
  105.     cmp    al,0AAh
  106.     je    @@Found_SB
  107. @@Not_Ready:
  108.     loop    @@Wait_Ready
  109.     mov    ax,0        ; Return false
  110.     ret
  111.  
  112. @@Found_SB:
  113.     mov    ax,165
  114.     push    ax
  115.     call    _dsp_time
  116.     pop    ax        ; ax!=0 so returns true
  117.     ret
  118. ENDP _dsp_reset
  119.  
  120. ;---------------------------------------------------------------------------
  121. ; void far dsp_dma_prepare(int Dir,int Length)
  122. ;---------------------------------------------------------------------------
  123. ; Dir = 0 for Microphone Input, 1 for Speaker Output
  124. ; Length = Length of data to be sampled/played
  125. ;---------------------------------------------------------------------------
  126. PROC _dsp_dma_prepare FAR
  127. ARG Dir:WORD,Len:WORD
  128.      push bp
  129.      mov  bp,sp
  130.      push ax dx
  131.  
  132.      await_DSP
  133.      mov  al,24h
  134.      cmp  [Dir],0
  135.        jz @@Is_Read
  136.      mov  al,14h
  137. @@Is_Read:
  138.      mov  dx,[DSP_Command]
  139.      out  dx,al
  140.      await_DSP
  141.      mov  ax,[Len]
  142.      out  dx,al
  143.      await_DSP
  144.      mov  al,ah
  145.      out  dx,al
  146.  
  147.      pop  dx ax
  148.      pop  bp
  149.      ret
  150. ENDP _dsp_dma_prepare
  151.  
  152. ;---------------------------------------------------------------------------
  153. ; void far dsp_voice(Activity)
  154. ;---------------------------------------------------------------------------
  155. ; Activity = 0 for silent, 1 for audible
  156. ;---------------------------------------------------------------------------
  157. PROC _dsp_voice FAR
  158. ARG Which:WORD
  159.      push bp
  160.      mov  bp,sp
  161.      push dx ax
  162.  
  163.      await_DSP
  164.      mov  ax,[Which]
  165.      and  al,1            ;Want a 1/0 parameter
  166.      xor  al,1
  167.      shl  al,1
  168.      or      al,0D1h
  169.      mov  dx,[DSP_Command]
  170.      out  dx,al
  171.      pop  ax dx
  172.      pop  bp
  173.      ret
  174. ENDP _dsp_voice
  175.  
  176. ;---------------------------------------------------------------------------
  177. ; void far set_SB_address(int base)
  178. ;---------------------------------------------------------------------------
  179. ; Sets base port of SoundBlaster that other functions refer to.
  180. ; Most likely value (and the default): 0x220
  181. ;---------------------------------------------------------------------------
  182. PROC _set_SB_address FAR
  183. ARG Port:WORD
  184.      push bp
  185.      mov  bp,sp
  186.      push ax
  187.      mov  ax,[Port]
  188.      mov  [SB_IO_Port],ax
  189.      add  ax,6
  190.      mov  [DSP_Reset],ax
  191.      add  ax,4
  192.      mov  [DSP_RDData],ax
  193.      add  ax,2
  194.      mov  [DSP_Command],ax
  195.      add  ax,2
  196.      mov  [DSP_RDAvail],ax
  197.      pop  ax
  198.      pop  bp
  199.      ret
  200. ENDP _set_SB_address
  201. END
  202.